﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AGE.Scene;
using AGE;
using AGE.Rendering;
using AGE.Physics;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
//using AGEImplementation.Behaviours;
namespace AGEImplementation
{
    class SpringDemo:Scene
    {
        int fpsAccum = 0;
        float total = 0;
        SpriteFont font;
        int fps;
        
        float accumulator;
        public override void Initialize(Microsoft.Xna.Framework.Graphics.GraphicsDevice graphicsDeviceIn)
        {
            AddEntity("MainCamera");
            Find("MainCamera").AddComponent(new Camera(graphicsDeviceIn.Viewport.AspectRatio));
            Find("MainCamera").Transform.Translate(0.0f, 0.0f, 20.0f);

            AddEntity("Center");
            AddEntity("North");
            AddEntity("South");
            AddEntity("East");
            AddEntity("West");
            //AddEntity("ReceiverA");
            //AddEntity("ReceiverB");
            //AddEntity("Sender");
            


            //Find("ReceiverA").AddComponent(new ReceiverBehaviour());
            //Find("ReceiverB").AddComponent(new ReceiverBehaviour());
            //Find("Sender").AddComponent(new SenderBehaviour());
            

            Find("Center").Transform.Translate(0.0f, 0.0f, 0.0f);
            Find("North").Transform.Translate(0.0f, 0.0f, 500.0f);
            Find("South").Transform.Translate(0.0f, 0.0f, -500.0f);
            Find("East").Transform.Translate(500.0f, 0.0f, 0.0f);
            Find("West").Transform.Translate(-500.0f, 0.0f, 0.0f);

            AddEntity("Rigidbody");
            Rigidbody rigid = new Rigidbody();
            rigid.Mass = 100f;
            Find("Rigidbody").AddComponent(rigid);

            AddEntity("Child");

            Find("Rigidbody").Transform.Translate(0.0f, 0.0f, 0.0f);
            

            Find("Rigidbody").Transform.Scale = new Vector3(0.01f, 0.01f, 0.01f);
            Find("Child").Transform.Translate(500.0f, 0.0f, 0.0f);
            Find("East").AttachChild(Find("Child"));
            Find("Center").Transform.Scale = new Vector3(0.01f, 0.01f, 0.01f);
            Find("Child").Transform.Scale = new Vector3(0.5f, 0.5f, 0.5f);
            Find("Center").AttachChild(Find("North"));
            Find("Center").AttachChild(Find("South"));
            Find("Center").AttachChild(Find("East"));
            Find("Center").AttachChild(Find("West"));

            base.Initialize(graphicsDeviceIn);
        }

        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager contentManager)
        {
            font = contentManager.Load<SpriteFont>("font");
            Model teapotModel = contentManager.Load<Model>("cube");

            Find("North").AddComponent(new ModelRenderer(teapotModel));
            Find("South").AddComponent(new ModelRenderer(teapotModel));
            Find("East").AddComponent(new ModelRenderer(teapotModel));
            Find("West").AddComponent(new ModelRenderer(teapotModel));
            Find("Child").AddComponent(new ModelRenderer(teapotModel));

            Find("Rigidbody").AddComponent(new ModelRenderer(teapotModel));
        }

        public override void Update(float deltaTime)
        {
            total += deltaTime;
            accumulator += deltaTime;
            KeyboardState keyboardState = Keyboard.GetState();

            Entity camera = Find("MainCamera");

            if (keyboardState.IsKeyDown(Keys.D))
            {
                camera.Transform.Rotate(Vector3.Up, -80.0f * deltaTime);
            }

            if (keyboardState.IsKeyDown(Keys.A))
            {
                camera.Transform.Rotate(Vector3.Up, 80.0f * deltaTime); 
            }

            if (keyboardState.IsKeyDown(Keys.W))
            {
                camera.Transform.Translate(camera.Transform.GetRotationMatrix().Forward * (8.0f * deltaTime));
            }

            if (keyboardState.IsKeyDown(Keys.S))
            {
                camera.Transform.Translate(camera.Transform.GetRotationMatrix().Forward * (-8.0f * deltaTime));
            }



            if (total > 1)
            {

                total = 0;
                fps = fpsAccum;
                fpsAccum = 0;
            }

            

            Find("Center").Transform.Rotate(Vector3.Up, 50.0f * deltaTime);
            //Find("East").Transform.Rotate(Vector3.Forward, 100.0f * deltaTime);

            Find("Rigidbody").GetComponent<Rigidbody>().AddForce(new Vector3(0.0f, -9.0f, 0.0f));
            base.Update(deltaTime);
        }

        public override void Draw(GraphicsDevice graphicsDevice, SpriteBatch spriteBatch)
        {
            base.Draw(graphicsDevice, spriteBatch);
            fpsAccum += 1;
            graphicsDevice.DepthStencilState = DepthStencilState.None;
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

            spriteBatch.DrawString(font, fps.ToString(), Vector2.Zero, Color.White);

            spriteBatch.DrawString(font, this.EntityCount.ToString(),new Vector2(0, 20f), Color.White);

            //spriteBatch.DrawString(font, accumulator.ToString(), new Vector2(0, 40f), Color.White);

            spriteBatch.DrawString(font, Find("Rigidbody").Transform.WorldPosition().ToString() + " MASS: " + Find("Rigidbody").GetComponent<Rigidbody>().Mass, new Vector2(0, 60f), Color.White);
            
            spriteBatch.End();
            
        }
    }
}
